<%@page import="java.awt.print.Printable"%>
<%@ page contentType="text/html;charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@ taglib prefix="tags" tagdir="/WEB-INF/tags"%>
<c:set var="ctx" value="${pageContext.request.contextPath}" />
<html>
<head>
<title>_${customer.customerName}_入库点收</title>
<script type="text/javascript">

  $(function() {
       //如果点收已经完成，隐藏操作按钮
	   var status='${task.status}';
	   if(status!='token' && status!='doing'){
	      $("button").hide();   
	   }
  });
  
  function keydown_1(){
	  $("#msg_1").html("");
  }
  
  function keydown_2(){
	  $("#msg_2").html("");
  }
  
  function sku1Change(){
	  $("#txt_boxCode").val('');
	  $("#txt_boxCode").focus();
  }
  
  function boxCodeChange(){
	  $("#txt_num").val('');
	  $("#txt_num").focus();
  }
  
  function numChange(){
	  var sku=$("#txt_sku_1").val().trim();
	  var num=$("#txt_num").val().trim();
	  var boxCode=$("#txt_boxCode").val().trim();
	  
	  if(sku.length==0){
		  $("#msg_1").css("color","red");
		  $("#msg_1").html("请输入sku!");
		  $("#txt_sku_1").select();
		  $("#txt_sku_1").focus();
		  return false;
	  }
	  
	  if(num.length==0){
		  $("#msg_1").css("color","red");
		  $("#msg_1").html("请输入数量!");
		  $("#txt_num").select();
		  $("#txt_num").focus();
		  return false;
	  }
	  
	  if(isNaN(num)){
		  $("#msg_1").css("color","red");
		  $("#msg_1").html("请输入正确的数字!");
		  $("#txt_num").select();
		  $("#txt_num").focus();
		  return false;
	  }
	  
	  
	  if(boxCode.length==0){
		  $("#msg_1").css("color","red");
		  $("#msg_1").html("请输入boxCode!");
		  $("#txt_boxCode").select();
		  $("#txt_boxCode").focus();
		  return false;
	  }
	  
	  var taskCode='${task.taskCode}';
	  
      $.ajax({
			type:'post',
			url:'${ctx}/v2_instorageCount/ajaxCount_box',
			data:{
				taskCode:taskCode,
				sku:sku,
				num:num,
				boxCode:boxCode
			},
			dataType:'json',
			success:function(data) {
				var status=data.status;
			    if( status=='success'){
			       
			       var itemList_box=data.itemList_box;
			       
			       $("#boxItem").html('');
			       var str='';
			       $.each(itemList_box,function(i,item){
			    	   str+="<tr id='tr_"+item.id+"'>";
			    	   str+="<td><input type='text' id='sku1_"+item.id+"' value='"+item.sku+"'/></td>";
			    	   str+="<td><input type='text' id='num1_"+item.id+"' value='"+item.num+"'/></td>";
			    	   str+="<td><input type='text' id='boxCode_"+item.id+"' value='"+item.boxCode+"'/></td>";
			    	   str+="<td><button class='btn btn-warning' onclick='deleteItem(\""+item.id+"\");'>删除</button><td>";
			    	   str+="</tr>";
			       });
			       $("#boxItem").html(str);
			    	
			       $("#txt_sku_1").val('');
			       $("#txt_num").val('');
			       $("#txt_boxCode").val('');
			   	   $("#txt_sku_1").focus();
			   	   
			       $("#msg_1").css("color","green");
				   $("#msg_1").html("点收成功。");
				  
			    }else{
			      alert("网络繁忙，请稍后重试...");
			    }
			},
			error:function() {
				alert("网络繁忙，请稍后重试...");
			}
	   });
  }
  
  function sku2Change(){
	  var sku=$("#txt_sku_2").val().trim();
	  
	  var taskCode='${task.taskCode}';
      $.ajax({
			type:'post',
			url:'${ctx}/v2_instorageCount/ajaxCount_Single',
			data:{
				taskCode:taskCode,
				sku:sku
			},
			dataType:'json',
			success:function(data) {
				var status=data.status;
			    if( status=='success'){
			       
			       var itemList_single=data.itemList_single;
			       
			       $("#singleItem").html('');
			       var str='';
			       $.each(itemList_single,function(i,item){
			    	   str+="<tr id='tr_"+item.id+"'>";
			    	   str+="<td><input type='text' id='sku2_"+item.id+"' value='"+item.sku+"'/></td>";
			    	   str+="<td><input type='text' id='num2_"+item.id+"' value='"+item.num+"'/></td>";
			    	   str+="<td><button class='btn btn-warning' onclick='deleteItem(\""+item.id+"\");'>删除</button></td>";
			    	   str+="</tr>";
			       });
			       $("#singleItem").html(str);
			    	
			       $("#txt_sku_2").val('');
			   	   $("#txt_sku_2").focus();
			   	   
			       $("#msg_2").css("color","green");
				   $("#msg_2").html("点收成功。");
				  
			    }else{
			      alert("网络繁忙，请稍后重试...");
			    }
			},
			error:function() {
				alert("网络繁忙，请稍后重试...");
			}
	   });
  }
  
  //删除
  function deleteItem(itemId){
	  $.ajax({
			type:'post',
			url:'${ctx}/v2_instorageCount/ajaxDeleteCountItem',
			data:{
				itemId:itemId
			},
			dataType:'json',
			success:function(data) {
			    if( data==true){
				   $("#tr_"+itemId).remove();
			    }else{
			      alert("网络繁忙，请稍后重试...");
			    }
			},
			error:function() {
				alert("网络繁忙，请稍后重试...");
			}
	   });
  }
  
  //完成点收
  function finish(){
      
     $("button").attr("disabled",true);
     $("button").attr("class","btn");
	 if($("#singleItem").children().length == 0 && $("#boxItem").children().length == 0){
	 	alert("点收数据不能为空");
	 	return;
	 }
  
	  $.ajax({
			type:'post',
			url:'${ctx}/v2_instorageCount/ajaxFinishCount',
			data:{
				taskCode:'${task.taskCode}',
				billCode:'${task.billCode}'
			},
			dataType:'json',
			success:function(data) {
			    if(data.result==true){
			    	window.location.href = "${ctx}/v2_instorageCount/toCountTaskListPage";
			    }else{
			      alert(data.content);
			      $("button").attr("disabled",false);
                   $("button").attr("class","btn btn-primary");
			    }
			},
			error:function() {
				alert("网络繁忙，请稍后重试...");
				$("button").attr("disabled",false);
                $("button").attr("class","btn btn-primary");
			}
	   });
  }
  function confirm(){
  	if($("#singleItem").children().length == 0 && $("#boxItem").children().length == 0){
	 	alert("点收数据不能为空");
	 	return;
	 }
  	window.location.href ="${ctx}/v2_instorageCount/toInstorageCountConfirmPage/${task.id}";
  }
</script>
</head>

<body>
<section class="content">

	<table id="contentTable" class="table  table-bordered table-condensed">
	    <thead>
			<th style="vertical-align: middle; color: white; " colspan="13" bgcolor="#3c8dbc">点收</th>
		</thead>
		<tr>
		    <td style="vertical-align: middle;text-align: center;">整箱点收</td>
		    <td > 
		       <span style="margin-left: 10px;">SKU:</span>
		       <input id="txt_sku_1" type="text" onchange="sku1Change();"  onkeydown="keydown_1();"/>
		       
		       <span style="margin-left: 10px;">库存箱码:</span>
		       <input id="txt_boxCode" type="text" onchange="boxCodeChange();"/>
		       
		       <span style="margin-left: 10px;">数量:</span>
		       <input id="txt_num" type="text" onchange="numChange();"/>
		       
		       
		       <span style="margin-left:12px; margin-top: 10px; color: red;font-weight: bold;" id="msg_1"></span>
		       
		    </td>
		</tr>
		<tr>
		    <td style="vertical-align: middle;text-align: center;">散货点收</td>
		    <td> 
		       <span style="margin-left: 10px;">SKU:</span>
		       <input id="txt_sku_2" type="text" onchange="sku2Change();" onkeydown="keydown_2();"/>
		       
		       <span style="margin-left:12px; margin-top: 10px; color: red;font-weight: bold;" id="msg_2"></span>
		    </td>
		</tr>
		<tr>
		    <td style="vertical-align: middle;text-align: center;">操作</td>
		    <td>
                 <%-- <button class="btn btn-primary" onclick="finish('${task.taskCode}');">完成点收</button> --%>      
                 <button class="btn btn-primary" onclick="confirm()">点收确认</button>
		    </td>
		</tr>
	</table>
	
	
	<!-- 整箱数据 -->
	<table class="table table-striped table-bordered table-condensed" style="margin-top: 0px;">
		<thead>
			<th style="vertical-align: middle; color: white; " colspan="13" bgcolor="#3c8dbc">整箱数据</th>
		</thead>
		<thead><tr>
			    <th style="width: 150px;">SKU</th>
			    <th style="width: 150px;">数量/箱</th>
			    <th style="width: 150px;">库位箱条码</th>
			    <th>操作</th>
			</tr></thead>
		<tbody id="boxItem">
		<c:forEach items="${itemList_box}" var="t" varStatus="i">
			<tr id="tr_${t.id}">
			   <td><input type="text" id="sku1_${t.id}" value="${t.sku}"/></td>
			   <td><input type="text" id="num1_${t.id}" value="${t.num}"/></td>
			   <td><input type="text" id="boxCode_${t.id}" value="${t.boxCode}"/></td>
			   <td><button class="btn btn-warning" onclick="deleteItem('${t.id}');">删除</button></td>
			</tr>
		</c:forEach>
		</tbody>
	</table>
	
	<!-- 散装数据 -->
	<table class="table table-striped table-bordered table-condensed" style="margin-top: 0px;">
		<thead>
			<th style="vertical-align: middle; color: white; " colspan="13" bgcolor="#3c8dbc">散装数据</th>
		</thead>
		<thead><tr>
		    <th style="width: 150px;">SKU</th>
		    <th style="width: 315px;">数量</th>
		    <th>操作</th>
		</tr></thead>
		<tbody id="singleItem">
		<c:forEach items="${itemList_single}" var="t" varStatus="i">
			<tr id="tr_${t.id}">
			   <td><input type="text" id="sku2_${t.id}" value="${t.sku}"/></td>
			   <td><input type="text" id="num2_${t.id}" value="${t.num}"/></td>
			   <td><button class="btn btn-warning" onclick="deleteItem('${t.id}');">删除</button></td>
			</tr>
		</c:forEach>
		</tbody>
		
	</table>
	
</section>
</body>
</html>
